package nz.co.doherty.gwtpbreadcrumbs.client.ui.common;

import com.google.gwt.inject.client.AsyncProvider;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.mvp.client.CodeSplitProvider;

public class ListItemFactory<Entity_, ListItemView_ extends ListItemView, ListItemPresenter_ extends ListItemPresenter<Entity_, ListItemView_>> {

	private final CodeSplitProvider<ListItemPresenter_> itemFactory;
	
	@Inject
	public ListItemFactory(AsyncProvider<ListItemPresenter_> itemFactory) {
		this.itemFactory = new CodeSplitProvider<ListItemPresenter_>(
				itemFactory);		
	}
	
	public void create(final Entity_ entity, final AsyncCallback<ListItemPresenter_> callback) {
		itemFactory.get(new AsyncCallback<ListItemPresenter_>() {

			@Override
			public void onSuccess(ListItemPresenter_ listItemPresenter) {
				listItemPresenter.init(entity);
				callback.onSuccess(listItemPresenter);
			}

			@Override
			public void onFailure(Throwable caught) {
				callback.onFailure(caught);
			}
		});
		
	}
	
	
}
